﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class CollectorEdit : Form
    {
        public CollectorEdit()
        {
            InitializeComponent();
        }

        private EventScavengerDAL eventScavengerDAL;
        private Collector selectedCollector = new Collector();
        public Collector SelectedCollector
        {
            get { return selectedCollector; }
            set { selectedCollector = value; }
        }

        public DialogResult Show(EventScavengerDAL eventScavengerDAL)
        {
            this.eventScavengerDAL = eventScavengerDAL;
            return ShowDialog();
        }

        #region Form events
        private void CollectorEdit_Load(object sender, EventArgs e)
        {
            LoadReapers();
            if (selectedCollector != null)
            {
                txtName.Text = selectedCollector.Name;
                numericUpDownMainFreq.Value = selectedCollector.MainThreadUpdateFrequencySec;
                numericUpDownSqlCmdTimeOutSec.Value = selectedCollector.SqlCommandTimeOutSec;
                chkImportsEnabled.Checked = selectedCollector.ImportsEnabled;
                numericUpDownImportFrequency.Value = selectedCollector.ImportFrequency;
                chkBatchInsertsEnabled.Checked = selectedCollector.UseBatchInserts;
                numericUpDownBatchInsertSize.Value = selectedCollector.InsertBatchSize;
                chkLogDuplicateWarnings.Checked = selectedCollector.LogDuplicateWarnings;
                chkEnabled.Checked = selectedCollector.Enabled;
                txtServiceName.Text = selectedCollector.ServiceName;
                txtServicePath.Text = selectedCollector.ServicePath;
                cboReaper.SelectedItem = (from Reaper r in cboReaper.Items
                                          where r.ReaperId == selectedCollector.ReaperId
                                          select r).FirstOrDefault();

                GlobalSettingsData globalSettingsData = eventScavengerDAL.GetGlobalSettings();
                if (globalSettingsData.MaintenanceRunner == selectedCollector.Name)
                    chkMaintenanceRunner.Checked = true;
                if (selectedCollector.Id > 0)
                    txtName.ReadOnly = true;
            }
        }


        #endregion

        #region Button events
        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Length == 0)
            {
                MessageBox.Show("You must specify a collector name!", "Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtName.Focus();
            }
            else if (cboReaper.SelectedIndex == -1)
            {
                MessageBox.Show("You must specify a Reaper (Server)!", "Reaper", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                cboReaper.Focus();
            }
            else if (txtServiceName.Text.Trim().Length == 0)
            {
                MessageBox.Show("You must specify a service name!", "Service name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtServiceName.Focus();
            }
            else if (txtServicePath.Text.Trim().Length == 0)
            {
                MessageBox.Show("You must specify a service path!", "Service name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtServicePath.Focus();
            }                
            else
            {
                selectedCollector.Name = txtName.Text;
                selectedCollector.MainThreadUpdateFrequencySec = (int)numericUpDownMainFreq.Value;
                selectedCollector.SqlCommandTimeOutSec = (int)numericUpDownSqlCmdTimeOutSec.Value;
                selectedCollector.ImportsEnabled = chkImportsEnabled.Checked;
                selectedCollector.ImportFrequency = (int)numericUpDownImportFrequency.Value;
                selectedCollector.UseBatchInserts = chkBatchInsertsEnabled.Checked;
                selectedCollector.InsertBatchSize = (int)numericUpDownBatchInsertSize.Value;
                selectedCollector.LogDuplicateWarnings = chkLogDuplicateWarnings.Checked;
                selectedCollector.Enabled = chkEnabled.Checked;
                selectedCollector.ReaperId = ((Reaper)cboReaper.SelectedItem).ReaperId;
                selectedCollector.ReaperName = ((Reaper)cboReaper.SelectedItem).ServerName;
                selectedCollector.ServiceName = txtServiceName.Text;
                selectedCollector.ServicePath = txtServicePath.Text;

                if (chkMaintenanceRunner.Checked) 
                {
                    GlobalSettingsData globalSettingsData = eventScavengerDAL.GetGlobalSettings();
                    globalSettingsData.MaintenanceRunner = selectedCollector.Name;
                    eventScavengerDAL.SetGlobalSettings(globalSettingsData);
                }

                DialogResult = DialogResult.OK;
                Close();
            }
        }
        #endregion

        #region Checkbox events
        private void chkImportsEnabled_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDownImportFrequency.Enabled = chkImportsEnabled.Checked;
        }
        #endregion        

        private void chkBatchInsertsEnabled_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDownBatchInsertSize.Enabled = chkBatchInsertsEnabled.Checked;
        }

        private void LoadReapers()
        {
            cboReaper.Items.Clear();
            foreach (Reaper r in eventScavengerDAL.GetReaperList())
            {
                cboReaper.Items.Add(r);
            }
        }

        private void cmdEditReapers_Click(object sender, EventArgs e)
        {
            Reapers reapers = new Reapers();
            reapers.Show(eventScavengerDAL);
            //if (reapers.Show(eventScavengerDAL) == System.Windows.Forms.DialogResult.OK)
            {
                LoadReapers();
                if (selectedCollector != null)
                {
                    cboReaper.SelectedItem = (from Reaper r in cboReaper.Items
                                              where r.ReaperId == selectedCollector.ReaperId
                                              select r).FirstOrDefault();
                }
            }
        }

        private void cmdRegistrationHelper_Click(object sender, EventArgs e)
        {
            ReaperSetupHelper reaperSetupHelper = new ReaperSetupHelper();

            selectedCollector.Name = txtName.Text;
            selectedCollector.ReaperName = cboReaper.Text;
            selectedCollector.ServiceName = txtServiceName.Text;
            selectedCollector.ServicePath = txtServicePath.Text;
            reaperSetupHelper.SelectedCollector = SelectedCollector;
            if (reaperSetupHelper.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SelectedCollector = reaperSetupHelper.SelectedCollector;
                txtName.Text = SelectedCollector.Name;
                cboReaper.Text = selectedCollector.ReaperName;
                txtServiceName.Text = SelectedCollector.ServiceName;
                txtServicePath.Text = SelectedCollector.ServicePath;
            }
        }

        private void cmdBrowseServicePath_Click(object sender, EventArgs e)
        {
            if (txtServicePath.Text.Length > 0)
            {
                openFileDialogEventReaper.FileName = txtServicePath.Text;
                openFileDialogEventReaper.InitialDirectory = System.IO.Path.GetDirectoryName(txtServicePath.Text);
            }
            if (openFileDialogEventReaper.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtServicePath.Text = openFileDialogEventReaper.FileName;
            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            if (selectedCollector.Id == 0)
            {
                txtServiceName.Text = "Event Reaper";
                if (txtName.Text.Length > 0)
                {
                    txtServiceName.Text += " - " + txtName.Text;
                }
            }
        }
    }
}
